<?php
require_once 'lib/generic_data.class.php';

class extjs_services extends generic_data {
    
    protected $generic_data_options     =   array();
       public $defaults                 =   array( 'textfield_length'  =>  25);
    
    /* Parent attributes for reference
    *
    *   protected $dsn          =   false;
    *   protected $dbh_options  =   false;
    *   protected $dbh          =   false;
    *   protected $database     =   false;
    *    
    *   // query tool specific attributes
    *   var $table          =   false;
    *   var $sequenceName   =   'generic';
    * 
    *   // this is default, but to demonstrate it here ...
    *   var $primaryCol     =   false;
    */
    
    function __construct($options = array()) {
        foreach($options as $attribute => $value) {
            if(isset($this->$attribute)) {
                $this->$attribute    =   $value;
            }
        }
        
        parent::__construct(array('dsn' =>  $this->dsn));
    }
    
    function get_textfield_length($length = false) {
         $length    =   ($length && !is_null($length) && !empty($length)) ?
                        $length : $this->defaults['textfield_length'];
                        
         $length    =   ($length < 40) ? 40 : $length;
         $length    =   ($length > 41 && $length < 255 ) ? 150 : $length;
         
         return $length;
    }
    
    function build_extjs_formpanel_items($table, $type = 'table') {
        if($type == 'table') {
            $this->db->loadModule('Reverse');
            $table_info =   $this->db->tableInfo($table);
            
            $rows       =   array();

            foreach($table_info as $attribute) {
                $rows[] =   array(  'fieldLabel'    =>  $attribute['name'],
                                    //'anchor' => '-90%',

                                    'name'          =>  $attribute['name'],
                                    'width'         =>  $this->get_textfield_length(@$attribute['length']),
                                                        
                                    'xtype'         =>  'textfield'
                                    );
            }
            
        // build form for custom view
        } else {
            
        }
        
        $response   =   array(  'success'   =>  true,
                                'metaData'  =>  array(  'root'      =>  'rows',
                                                        'id'        =>  $table.'_form',
                                                        'fields'    =>  array_keys($rows[0])
                                                        ),
                                'rows'      =>  $rows                
                                );
                                
        return json_encode($response);
                                                
    }
    
    /** fetch_json_formpanel_result
    *
    *   This method is used for fetching data from a single table.
    *
    *   @param $attribute - primary key (str), $value (str)
    *   @return array
    *   @author david_yerrington@yahoo.com
    */
    function fetch_json_formpanel_table_result($attribute, $value) {
        $this->set_querytool_attributes($table, $attribute);
    }

}

?>
